/* =============================================================================
 * BeeWorks MoneyPile - Domain Model
 *
 * 
 * =============================================================================
 *
 * Developers: Jurgen Lust
 *
 * 
 * (C) Copyright 2009, BeeWorks BVBA.
 *
 */

package be.beeworks.moneypile.model;

import be.beeworks.moneypile.model.enums.InvoiceDetailType;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

import java.io.Serializable;

import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


/**
 * A detail on an invoice
 *
 * @author jlust
 * @author $LastChangedBy: Jurgen.Lust $
 *
 * @version $LastChangedRevision: 15 $
 */
@Entity
@Table(name = "INVOICE_DETAILS")
@Indexed
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class InvoiceDetail
        implements Serializable {
    //~ Static fields/initializers ---------------------------------------------

    private static final long serialVersionUID = -5100395171948628310L;

    //~ Instance fields --------------------------------------------------------

    @NotNull
    @Min(1)
    private Integer unitCount;
    @NotNull
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinColumn(name = "invoice_id_ref", nullable = false)
    @XmlIDREF
    private Invoice invoice;
    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private InvoiceDetailType unitType;
    @Version
    @XmlTransient
    private Long version;
    @Id
    @XmlID
    @DocumentId
    @Min(32)
    @Max(32)
    private String id;
    @NotNull
    @Size(min = 1, max = 100)
    @Field(index = Index.TOKENIZED, store = Store.NO)
    private String unitName;

    //~ Constructors -----------------------------------------------------------

    public InvoiceDetail() {
        super();
        this.id = UUID.randomUUID().toString().replaceAll("-", "");
    }

    //~ Methods ----------------------------------------------------------------

    public InvoiceDetailType getUnitType() {
        return unitType;
    }

    public void setUnitType(InvoiceDetailType unitType) {
        this.unitType = unitType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getVersion() {
        return version;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(final Invoice invoice) {
        this.invoice = invoice;
    }

    public String getUnitName() {
        return unitName;
    }

    public void setUnitName(final String unitName) {
        this.unitName = unitName;
    }

    public Integer getUnitCount() {
        return unitCount;
    }

    public void setUnitCount(final Integer unitCount) {
        this.unitCount = unitCount;
    }

    public String getUnitTypeString() {
        return (getUnitType() == null) ? "" : getUnitType().name();
    }

    public Float getPriceWithoutVat() {
        switch (getUnitType()) {
            case HOLIDAY:
                return getUnitCount() * getInvoice().getProject()
                                            .getHolidayHourRate();

            case OVERTIME:
                return getUnitCount() * getInvoice().getProject()
                                            .getOvertimeHourRate();

            default:
                return getUnitCount() * getInvoice().getProject().getHourRate();
        }
    }
}
